home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / computerjanitor / plugin.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  6.9 KB  |  172 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import imp
  5. import inspect
  6. import logging
  7. import os
  8. import computerjanitor
  9. _ = computerjanitor.setup_gettext()
  10. import logging
  11.  
  12. class Plugin(object):
  13.     """Base class for plugins.
  14.     
  15.     These plugins only do one thing: identify cruft. See the 'get_cruft'
  16.     method for details.
  17.     
  18.     """
  19.     
  20.     def get_condition(self):
  21.         if hasattr(self, '_condition'):
  22.             return self._condition
  23.         return []
  24.  
  25.     
  26.     def set_condition(self, condition):
  27.         self._condition = condition
  28.  
  29.     condition = property(get_condition, set_condition)
  30.     
  31.     def set_application(self, app):
  32.         """Set the Application instance this plugin belongs to.
  33.         
  34.         This is used by the plugin manager when creating the plugin
  35.         instance. In a perfect world, this would be done via the
  36.         __init__ method, but since I took a wrong left turn, I ended
  37.         up in an imperfect world, and therefore giving the Application
  38.         instance to __init__ would mandate that all sub-classes would
  39.         have to deal with that explicitly. That is a lot of unnecessary
  40.         make-work, which we should avoid. Therefore, we do it via this
  41.         method.
  42.         
  43.         The class may access the Application instance via the
  44.         'app' attribute.
  45.         
  46.         """
  47.         self.app = app
  48.  
  49.     
  50.     def do_cleanup_cruft(self):
  51.         '''Find cruft and clean it up.
  52.  
  53.         This is a helper method.
  54.         '''
  55.         for cruft in self.get_cruft():
  56.             cruft.cleanup()
  57.         
  58.         self.post_cleanup()
  59.  
  60.     
  61.     def get_cruft(self):
  62.         """Find some cruft in the system.
  63.         
  64.         This method MUST return an iterator (see 'yield' statement).
  65.         This interface design allows cruft to be collected piecemeal,
  66.         which makes it easier to show progress in the user interface.
  67.         
  68.         The base class default implementation of this raises an
  69.         exception. Subclasses MUST override this method.
  70.  
  71.         """
  72.         raise computerjanitor.UnimplementedMethod(self.get_cruft)
  73.  
  74.     
  75.     def post_cleanup(self):
  76.         '''Does plugin wide cleanup after the individual cleanup
  77.            was performed.
  78.            
  79.            This is useful for stuff that needs to be proccessed
  80.            in batches (e.g. for performance reasons) like package
  81.            removal.
  82.         '''
  83.         pass
  84.  
  85.  
  86.  
  87. class PluginManager(object):
  88.     """Class to find and load plugins.
  89.     
  90.     Plugins are stored in files named '*_plugin.py' in the list of
  91.     directories given to the initializer.
  92.     
  93.     """
  94.     
  95.     def __init__(self, app, plugin_dirs):
  96.         self._app = app
  97.         self._plugin_dirs = plugin_dirs
  98.         self._plugins = None
  99.  
  100.     
  101.     def get_plugin_files(self):
  102.         '''Return all filenames in which plugins may be stored.'''
  103.         names = []
  104.         for dirname in self._plugin_dirs:
  105.             basenames = _[1]
  106.             logging.debug('Plugin modules in %s: %s' % (dirname, ' '.join(basenames)))
  107.             [] += [ os.path.join(dirname, x) for x in basenames ]
  108.         
  109.         return names
  110.  
  111.     
  112.     def _find_plugins(self, module):
  113.         '''Find and instantiate all plugins in a module.'''
  114.         plugins = []
  115.         for dummy, member in inspect.getmembers(module):
  116.             if inspect.isclass(member) and issubclass(member, Plugin):
  117.                 plugins.append(member)
  118.                 continue
  119.         
  120.         logging.debug('Plugins in %s: %s' % (module, ' '.join((lambda .0: for x in .0:
  121. str(x))(plugins))))
  122.         return [ plugin() for plugin in plugins ]
  123.  
  124.     
  125.     def _load_module(self, filename):
  126.         '''Load a module from a filename.'''
  127.         logging.debug('Loading module %s' % filename)
  128.         (module_name, dummy) = os.path.splitext(os.path.basename(filename))
  129.         f = file(filename, 'r')
  130.         
  131.         try:
  132.             module = imp.load_module(module_name, f, filename, ('.py', 'r', imp.PY_SOURCE))
  133.         except Exception:
  134.             e = None
  135.             logging.warning("Failed to load plugin '%s' (%s)" % (module_name, e))
  136.             return None
  137.  
  138.         f.close()
  139.         return module
  140.  
  141.     
  142.     def get_plugins(self, condition = [], callback = None):
  143.         '''Return all plugins that have been found.
  144.         
  145.         If callback is specified, it is called after each plugin has
  146.         been found, with the following arguments: filename, index of
  147.         filename in list of files to be examined (starting with 0), and
  148.         total number of files to be examined. The purpose of this is to
  149.         allow the callback to inform the user in case things take a long
  150.         time.
  151.         
  152.         '''
  153.         if self._plugins is None:
  154.             self._plugins = []
  155.             filenames = self.get_plugin_files()
  156.             for i in range(len(filenames)):
  157.                 if callback:
  158.                     callback(filenames[i], i, len(filenames))
  159.                 
  160.                 module = self._load_module(filenames[i])
  161.                 for plugin in self._find_plugins(module):
  162.                     plugin.set_application(self._app)
  163.                     self._plugins.append(plugin)
  164.                 
  165.             
  166.         
  167.         plugins = _[1]
  168.         logging.debug("plugins for condition '%s' are '%s'" % (condition, plugins))
  169.         return plugins
  170.  
  171.  
  172.